public class LCR146 {

    public static void main(String[] args) {
        int[][] ints = {{2, 3, 7}};
//        int[][] ints = {{1, 2, 3}, {8, 9, 4}, {7, 6, 5}};
        int[] ints1 = new Solution().spiralArray(ints);

        for (int i : ints1) {
            System.out.print(i);
            System.out.print(",");
        }
    }


    static class Solution {
        public int[] spiralArray(int[][] array) {
            if (array.length == 0) {
                return new int[0];
            }

            int xLength = array.length;
            int yLength = array[0].length;

            int[] arr = new int[yLength * xLength];
            int xBegin = 0, yBegin = 0, xEnd = xLength - 1, yEnd = yLength - 1;
            int index = 0;

            while (true) {
                for (int i = yBegin; i <= yEnd; i++) {
                    arr[index++] = array[xBegin][i];
                }
                xBegin++;
                if (index >= xLength * yLength) {
                    break;
                }
                for (int i = xBegin; i <= xEnd; i++) {
                    arr[index++] = array[i][yEnd];
                }
                yEnd--;
                if (index >= xLength * yLength) {
                    break;
                }
                for (int i = yEnd; i >= yBegin; i--) {
                    arr[index++] = array[xEnd][i];
                }
                xEnd--;
                if (index >= xLength * yLength) {
                    break;
                }
                for (int i = xEnd; i >= xBegin; i--) {
                    arr[index++] = array[i][yBegin];
                }
                yBegin++;
                if (index >= xLength * yLength) {
                    break;
                }
            }
            return arr;
        }
    }
}
